// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


  function geocodePosition(pos) {
    var geocoder = new google.maps.Geocoder();

    geocoder.geocode({
      latLng: pos
    }, function(responses) {
      if (responses && responses.length > 0) {
        updateMarkerAddress(responses[0].formatted_address);
      } else {
        updateMarkerAddress('Cannot determine address at this location.');
      }
    });
  }

  function updateMarkerPosition(latLng) {
    document.getElementById('latitud').value = latLng.lat();
    document.getElementById('longitud').value = latLng.lng();
    
    }

  function initialize() {

       if (document.getElementById('marcadores')){
              var marcadores = document.getElementById('marcadores').value;
                
                    var latlng = new google.maps.LatLng(document.getElementById('latitude1').value, document.getElementById('longitude1').value);
                    var myOptions = {zoom: 8,center: latlng,mapTypeId: google.maps.MapTypeId.ROADMAP};
                     map = new google.maps.Map(document.getElementById('mapa'), myOptions);
                      
                for (i=1;i<=marcadores;i=i+1){
                    latlng = new google.maps.LatLng(document.getElementById('latitude'+i).value, document.getElementById('longitude'+i).value);
                     marker = new google.maps.Marker({
                     position: latlng,
                     draggable: true,
                     map: map
                    })
                }
        }
        else {
               
               var latlng = new google.maps.LatLng(10.46721252135467, -66.97320150000019);
              var myOptions = {zoom: 8,center: latlng,mapTypeId: google.maps.MapTypeId.ROADMAP};
              map = new google.maps.Map(document.getElementById('mapa'), myOptions);
              var marker = new google.maps.Marker({
              position: latlng,
              draggable: true,
              map: map
             })
        }
        updateMarkerPosition(latlng);
        google.maps.event.addListener(marker, 'dragend', function() {
        updateMarkerPosition(marker.getPosition());
        });
        
}

function eliminar_campos(link){
    $(link).previous("input[type=hidden]").value = "1";
    $(link).up(".fields").hide();
}

function agregar_campos(link, association,content){
   var new_id = new Date().getTime();
    var regexp = new RegExp("new_"+association,"g")
    $(link).up().insert({
       before: content.replace(regexp,new_id)
    });
}


